<template>
    <a-modal
      :title="(model&&model.id) ? '编辑': '新增'"
      :width="900"
      :height="360"
      :visible="visible"
      :confirmLoading="loading"
      @ok="handleOK"
      @cancel="() => { $emit('cancel') }">
      <a-spin :spinning="loading">
        <a-form :form="form" v-bind="formLayout">
          <a-form-item label="id" style="display: none;">
            <a-input v-decorator="['id']" />
          </a-form-item>
          <a-form-item label="名称">
            <a-input v-decorator="['name', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
          </a-form-item>
          <a-form-item label="价格 / 元">
            <a-input v-decorator="['price', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
          </a-form-item>
          <a-form-item label="有效期">
            <a-input v-decorator="['days', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
          </a-form-item>
          <a-form-item label="每日费用提示">
            <a-input v-decorator="['note', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
          </a-form-item>
          <a-form-item label="排序权重">
            <a-input v-decorator="['sortOrder', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
          </a-form-item>
        </a-form>
      </a-spin>
    </a-modal>
  </template>
  
    <script>
    import pick from 'lodash.pick'
    // 表单字段
    const fields = ['name', 'id', 'price', 'days', 'note', 'sortOrder']
    export default {
      props: {
        visible: {
          type: Boolean,
          required: true
        },
        loading: {
          type: Boolean,
          default: () => false
        },
        model: {
          type: Object,
          default: () => null
        }
      },
      data () {
        this.formLayout = {
          labelCol: {
            xs: { span: 24 },
            sm: { span: 3 }
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 18 }
          }
        }
        return {
          form: this.$form.createForm(this),
          uploadloading: false,
        }
      },
      created () {
        // 防止表单未注册
        fields.forEach(v => this.form.getFieldDecorator(v))
  
        // 当 model 发生改变时，为表单设置值
        this.$watch('model', () => {
          if (this.model) {
            this.form.setFieldsValue(pick(this.model, fields))
          }
        })
      },
      methods: {
        handleOK () {
          this.$emit('ok')
        }
      }
    }
    </script>
    <style scoped>
    .avatar-uploader > .ant-upload {
      width: 128px;
      height: 128px;
    }
    .ant-upload-select-picture-card i {
      font-size: 32px;
      color: #999;
    }
  
    .ant-upload-select-picture-card .ant-upload-text {
      margin-top: 8px;
      color: #666;
    }
    </style>
  